package com.tron.dobest.redistemplate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;

/**
 * 描述：
 *
 * @author TRON
 * @since 2020-04-22
 */
@RestController
public class RedisTestController {
    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("/redisTest01")
    public Object redisTest01() {
        //简单数据的处理
        ValueOperations valueOperations = redisTemplate.opsForValue();
        ListOperations listOperations = redisTemplate.opsForList();
        SetOperations setOperations = redisTemplate.opsForSet();
        HashOperations hashOperations = redisTemplate.opsForHash();

        valueOperations.set("a","中文");  //默认TTL=-1 永久有效
        System.out.println(valueOperations.get("a"));
        valueOperations.set("b", "中文", 17); //offset指定存储长度，数据变为十六进制
        valueOperations.set("c","中文", Duration.ofHours(1));  //存储一个小时
        valueOperations.append("d","中文");  //在当前key追加内容
        valueOperations.set("e",10);
        valueOperations.decrement("e"); //将当前key值中的integer值减1
        valueOperations.increment("e", 10); //将当前key值中的integer值加10
        valueOperations.getAndSet("e",6); //取出当前key的值，替换成新值

        return "";
    }
}
